<?php
namespace App\Service\H5;

class TongjiService1017 extends BaseService
{
    //统计

    //工时统计
    public static function tongji($type, $uid)
    {
        if ($type == 1) {
            $arys = self::tji01($uid);
        } else if ($type == 2) {
            $arys = self::tji02($uid);
        } else if ($type == 3) {
            $arys = self::tji03();
        } else if ($type == 4) {
            $arys = self::tji04($uid);
        } else {
            return self::toback(-2, '没有数据');
        }
        return self::toback(0, '', $arys);
    }

    public static function tji01($uid)
    {
        $arys = [
            'title' => [],
            'val' => [],
        ];
        $startDate = date('Y-m-d', time());
        for ($i = 0; $i < 5; $i++) {
            $day = date('Y-m-d', strtotime("$startDate +$i day"));
            $startday = $day.' 00:00:01';
            $endday = $day.' 23:59:59';
            $arys['title'][] = $day;
            $delay = 0;
            $whs = self::dbConn(self::$dbYwWh)
                ->where('del', 0)
                ->where('staff_id', $uid)
                ->where('status', '>', 1)
                ->where('addtime', '>', $startday)
                ->where('addtime', '<', $endday)
                ->get();
            if (count($whs)) {
                foreach ($whs as $v) {
                    $delay += strtotime($v->endhour) - strtotime($v->starthour);
                }
            }
            $arys['val'][] = ceil($delay / 60);
        }
        return $arys;
    }

    public static function tji02($uid)
    {
        $arys = [
            'title' => [],
            'val' => [],
        ];
        $whMys = self::dbConn(self::$dbYwWh)
            ->where('del', 0)
            ->where('staff_id', $uid)
            ->where('status', '>', 1)
            ->groupBy('item_id')
            ->get();
        $itemids = [];
        if (!count($whMys)) {
            return ['title'=>['没有项目'],'val'=>[0]];
        }
        foreach ($whMys as $v) {
            $itemids[] = $v->id;
        }
        $itemOnes = self::dbConn(self::$dbConfigItem)
            ->where('del', 0)
            ->whereIn('id', $itemids)
            ->get();
        if (count($itemOnes)) {
            foreach ($itemOnes as $v) {
                $arys['title'][] = $v->title;
                $whs = self::dbConn(self::$dbYwWh)
                    ->where('del', 0)
                    ->where('item_id', $v->id)
                    ->where('status', '>', 1)
                    ->get();
                $delay = 0;
                if (count($whs)) {
                    foreach ($whs as $vv) {
                        $delay += strtotime($vv->endhour) - strtotime($vv->starthour);
                    }
                }
                $delay = ceil($delay / 60);
                $arys['val'][] = $delay ? $delay : 1;
            }
        }
        return $arys;
    }

    public static function tji03()
    {
        $arys = [
            'title' => [],
            'val' => [],
        ];
        $itemOnes = self::dbConn(self::$dbConfigItem)
            ->where('del', 0)
            ->get();
        if (count($itemOnes)) {
            foreach ($itemOnes as $v) {
                $arys['title'][] = $v->title;
                $whs = self::dbConn(self::$dbYwWh)
                    ->where('del', 0)
                    ->where('item_id', $v->id)
                    ->where('status', '>', 1)
                    ->get();
                $delay = 0;
                if (count($whs)) {
                    foreach ($whs as $vv) {
                        $delay += strtotime($vv->endhour) - strtotime($vv->starthour);
                    }
                }
                $arys['val'][] = ceil($delay / 60);
            }
        }
        return $arys;
    }

    public static function tji04($uid)
    {
        $arys = [
            'title' => [],
            'val' => [],
            'valPre' => [],
        ];
        $startDate = date('Y-m-d', time());
        for ($i = 0; $i < 5; $i++) {
            $day = date('Y-m-d', strtotime("$startDate +$i day"));
            $startday = $day.' 00:00:01';
            $endday = $day.' 23:59:59';
            $arys['title'][] = $day;
            $delay = 0;
            $whs = self::dbConn(self::$dbYwWh)
                ->where('del', 0)
                ->where('staff_id', $uid)
                ->where('status', '>', 1)
                ->where('addtime', '>', $startday)
                ->where('addtime', '<', $endday)
                ->get();
            if (count($whs)) {
                foreach ($whs as $v) {
                    $delay += strtotime($v->endhour) - strtotime($v->starthour);
                }
            }
            $arys['val'][] = ceil($delay / 60);
        }
        for ($i = 5; $i < 9; $i++) {
            $day = date('Y-m-d', strtotime("$startDate +$i day"));
            $startday = $day.' 00:00:01';
            $endday = $day.' 23:59:59';
            $arys['title'][] = $day;
            $delay = 0;
            $whs = self::dbConn(self::$dbYwWh)
                ->where('del', 0)
                ->where('staff_id', $uid)
                ->where('status', '>', 1)
                ->where('addtime', '>', $startday)
                ->where('addtime', '<', $endday)
                ->get();
            if (count($whs)) {
                foreach ($whs as $v) {
                    $delay += strtotime($v->endhour) - strtotime($v->starthour);
                }
            }
            $arys['valPre'][] = ceil($delay / 60) + 1;
        }
        return $arys;
    }

//    //工时汇总
//    public static function whtj($day, $type, $uid, $item_id)
//    {
//        $delay = 0;
//        if ($type == 1) {
//        }
//        $startday = $day.' 00:00:01';
//        $endday = $day.' 23:59:59';
//        $db = self::dbConn(self::$dbYwWh)
//            ->where('del', 0)
//            ->where('status', '>', 1)
//            ->where('addtime', '>', $startday)
//            ->where('addtime', '<', $endday);
//        $db = self::dbConn(self::$dbYwWh)
//            ->where('del', 0)
//            ->where('status', '>', 1)
//            ->where('addtime', '>', $startday)
//            ->where('addtime', '<', $endday);
//        if ($type == 1) {
//            $db = $db->where('staff_id', $uid);
//        }
//        if ($type == 2) {
//            $db = $db->where('item_id', $item_id);
//        }
//        if ($type == 3) {
//            $db = $db->where('item_id', $item_id);
//        }
//        $res = $db->get();
//        if (!count($res)) {
//            return $delay;
//        }
//        foreach ($res as $v) {
//            $delay += strtotime($v->endhour) - strtotime($v->starthour);
//        }
//        return ceil($delay / 60);
//    }
}